<template>
  <a-modal
    :title="`${ okHidden ? '' : model && model.id ? '编辑' : '新增'}服务分类`"
    :ok-button-props="{ style: okHidden && { display: 'none' } }"
    :width="680"
    :visible="visible"
    :confirmLoading="loading"
    :maskClosable="false"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-if="model && model.id" label="主键ID" hidden>
          <a-input v-decorator="['id', { initialValue: model.id }]" disabled />
        </a-form-item>
        <a-form-item label="名称">
          <a-input :disabled="okHidden" placeholder="请输入名称" v-decorator="['service_name', {rules: [{required: true, message: '服务分类名称不能为空！' }, {max: 28, message: '最大可输入28个字符！' }]}]" />
        </a-form-item>
        <a-form-item label="图标" >
          <Upload :showUploadList="{ showRemoveIcon: !okHidden }" :style="{ marginLeft: !okHidden ? '0px' : '10px' }" :maxLength="1" v-decorator="['image', {rules: [{required: true, message: '请上传封面！' }]}]"></Upload>
          <div :style="{ marginLeft: !okHidden ? '0px' : '10px' }">支持jpg、jpeg、png格式，只能上传一张图片</div>
        </a-form-item>
        <a-form-item label="分类展示位">
          <!-- 别问我为什么不做循环，1是懒 2是这antd-vue 1.x设计的是真xx -->
          <a-select :disabled="okHidden" placeholder="请选择分类展示位" v-decorator="['objects', {rules: [{required: true, message: '请选择至少一个分类展示位！'}]}]" >
            <a-select-option :value="0">
              村内
            </a-select-option>
            <a-select-option :value="1">
              村外
            </a-select-option>
            <a-select-option :value="2">
              全部
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="简介">
          <a-textarea
            :disabled="okHidden"
            @input="textareaChange"
            :maxLength="200"
            autoSize
            style="min-height: 150px;resize: none;margin-top: 5px;"
            placeholder="请输入简介"
            v-decorator="['explain', {rules: [{required: true, message: '简介不能为空！' }]}]"
          />
          <text-count v-if="!okHidden" :maxLength="200" :currentLength="currentLength"></text-count>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import Upload from '@/components/Upload/Upload'

// 表单字段
const fields = ['objects', 'service_name', 'image', 'explain']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    okHidden: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      }
    }
    return {
      form: this.$form.createForm(this),
      currentLength: 0
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.$nextTick(() => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    })
  },
  methods: {
    textareaChange () {
      this.form.validateFields((errors, values) => {
        this.currentLength = values.explain.length
      })
    }
  },
  components: {
    Upload
  }
}
</script>

<style scoped>
  >>> .ant-upload.ant-upload-select-picture-card {
    margin-bottom: 0
  }
  >>> .clearfix {
    height: 110px
  }
  >>> .remove70 {
    margin-bottom: 110px !important;
    height: 300px !important;
  }
</style>
